package com.wmd.validation.util;
/**
*类说明
*@author yincheng lu
*@time 2018年12月27日上午11:38:11
*@Description
*/

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LogMessage {
	private static final Log logger=LogFactory.getLog(LogMessage.class);
	public static final String FILE_SUFFIX = "_file";


	private int code;//返回状态
	private String requestPath = null; // 请求地址
	private String userName = null; // 用户名
	private String user_id;//用户ID
	private Map<?, ?> inputParamMap = null; // 传入参数
	private Map<String,FileInfo> inputFileParamMap=null;//文件参数
	private Object outputParamMap = null; // 返回结果map格式
	private long startTimeMillis = 0; // 开始时间
	private long endTimeMillis = 0; // 结束时间
	private String methodName;//方法注释
	private List<Object> arguments;//方法参数

	public List<Object> getArguments() {
		return arguments;
	}

	public void setArguments(List<Object> arguments) {
		this.arguments = arguments;
	}

	public String getRequestPath() {
		return requestPath;
	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getUser_id() {
		return user_id;
	}

	public Map<String, FileInfo> getInputFileParamMap() {
		return inputFileParamMap;
	}

	public void setInputFileParamMap(Map<String, MultipartFile> MultipartFiles) {
		
		if(MultipartFiles!=null) {
			Map<String,FileInfo> fileInfos=new HashMap<>();
			MultipartFiles.forEach((k,v)->{
				MultipartFile mf=(MultipartFile)v;
				FileInfo  fi=new FileInfo();
				fi.fileName=k.substring(0,k.length()-FILE_SUFFIX.length());
				fi.realName=mf.getOriginalFilename();
				fi.size=mf.getSize();
				fi.type=fi.realName.split("\\.")[1];
				fileInfos.put(fi.fileName, fi);
				//System.out.println(fi.toString());
			});
			this.inputFileParamMap=fileInfos;
		}else {
			//System.out.println("map is null!");
		}
	}

	public void setUser_id(String user_id) {
		this.user_id = user_id;
	}

	public void setRequestPath(String requestPath) {
		this.requestPath = requestPath;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public Map<?, ?> getInputParamMap() {
		return inputParamMap;
	}

	public void setInputParamMap(Map<?, ?> inputParamMap) {
		
		this.inputParamMap = inputParamMap;
	}

	public Object getOutputParamMap() {
		return outputParamMap;
	}

	public void setOutputParamMap(Object outputParamMap) {
		this.outputParamMap = outputParamMap;
	}

	public long getStartTimeMillis() {
		return startTimeMillis;
	}

	public void setStartTimeMillis(long startTimeMillis) {
		this.startTimeMillis = startTimeMillis;
	}

	public long getEndTimeMillis() {
		return endTimeMillis;
	}

	public void setEndTimeMillis(long endTimeMillis) {
		this.endTimeMillis = endTimeMillis;
	}

	public String getMethodName() {
		return methodName;
	}

	public void setMethodName(String methodName) {
		this.methodName = methodName;
	}
	

	public JSONObject toJSONObject() {
		return JSON.parseObject(JSON.toJSONString(this));
	}
	
	public void printLog() {
		//String optTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(getStartTimeMillis());
		logger.debug("--------> user：" + getUserName() + "  url：" + getRequestPath()
				+ "; pro_time：" + (getEndTimeMillis() - getStartTimeMillis()) + "ms ;" + "method:"
				+ getMethodName() +"; arguments: "+JSON.toJSONString(getArguments())+ "; param：" + JSON.toJSONString(getInputParamMap()) + ";"+" fileParams: "+JSON.toJSONString(getInputFileParamMap()) + "; result："
				+ JSON.toJSONString(getOutputParamMap()));
	}
	class FileInfo {
		public String fileName;
		public long size;
		public String realName;
		public String type;
		@Override
		public String toString() {
			return "FileInfo [fileName=" + fileName + ", size=" + size + ", realName=" + realName + ", type=" + type
					+ "]";
		}
		
	}
}
